package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.entity.*;
import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.*;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.ClockItemService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.vo.request.SelectBellItemListVo;
import com.rm.zeyou.vo.request.SelectItemListVo;
import com.rm.zeyou.vo.response.SelectItemDetailResponseVo;
import com.rm.zeyou.vo.response.SelectUserCollectionListResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 加钟服务项目表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:07
 */
@Service
public class ClockItemServiceImpl extends ServiceImpl<ClockItemMapper, ClockItem> implements ClockItemService {
    @Resource
    private ClockItemMapper clockItemMapper;
    @Resource
    private UserItemOrderMapper userItemOrderMapper;
    @Resource
    private UserMemberMapper userMemberMapper;
    @Resource
    private UserCollectionMapper userCollectionMapper;
    @Resource
    private ClockItemSpecsMapper clockItemSpecsMapper;

    @Override
    public ApiRestResponse<PageInfo<SelectUserCollectionListResponseVo>> selectBellItemList(Long userId, SelectBellItemListVo selectBellItemListVo) {
        UserItemOrder userItemOrder = userItemOrderMapper.selectOneByOrderNo(selectBellItemListVo.getOrderNo());
        //UserItemOrder userItemOrder = userItemOrderMapper.selectOne(new LambdaQueryWrapper<UserItemOrder>()
        //        .eq(UserItemOrder::getOrderNo, selectBellItemListVo.getOrderNo()));
        return ApiRestResponse.success(PageHelper.startPage(selectBellItemListVo.getPageNum(), selectBellItemListVo.getPageSize())
                .doSelectPageInfo(() -> clockItemMapper.selectBellItemList(userItemOrder.getTid())));
    }

    @Override
    public ApiRestResponse<SelectItemDetailResponseVo> selectBellItemDetail(Long userId, SelectItemListVo selectVo) {
        ClockItem clockItem = clockItemMapper.selectById(selectVo.getItemId());
        if (CheckEmptyUtil.isEmpty(clockItem)) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.ITEM_EMPTY);
        }
        SelectItemDetailResponseVo vo = new SelectItemDetailResponseVo();
        vo.setItemName(clockItem.getItemName());
        vo.setItemBanner(clockItem.getItemBanner());
        vo.setItemTime(clockItem.getItemTime());
        vo.setItemIntroduction(clockItem.getItemIntroduction());
        vo.setItemFeatures(clockItem.getItemFeatures());
        vo.setItemDescription(clockItem.getItemDescription());
        vo.setItemOrdersSold(clockItem.getRealSalesCount() + clockItem.getVirtualSalesCount());
        vo.setItemPrice(clockItem.getItemPrice());
        vo.setItemMemberPrice(clockItem.getItemMemberPrice());
        int isVip = userMemberMapper.selectCount(new LambdaQueryWrapper<UserMember>()
                .eq(UserMember::getUserId, userId)).intValue();
        vo.setMemberStatus(isVip);
        int isCollect = userCollectionMapper.selectCount(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
                .eq(UserCollection::getItemId, selectVo.getItemId())).intValue();
        vo.setCollectionStatus(isCollect);


        vo.setDiscountPrice(clockItem.getItemPrice().subtract(clockItem.getItemMemberPrice()));

        List<ItemSpecs> itemSpecsList = new ArrayList<>();
        List<ClockItemSpecs> clockItemSpecss = clockItemSpecsMapper.selectList(new LambdaQueryWrapper<ClockItemSpecs>()
                .eq(ClockItemSpecs::getClockItemId, selectVo.getItemId()));
        if (!CheckEmptyUtil.isEmpty(clockItemSpecss)) {
            for (ClockItemSpecs clockItemSpecs : clockItemSpecss) {
                ItemSpecs itemSpecs = new ItemSpecs();
                itemSpecs.setItemId(clockItemSpecs.getClockItemId());
                itemSpecs.setId(clockItemSpecs.getId());
                itemSpecs.setItemSpecsName(clockItemSpecs.getItemSpecsName());
                itemSpecs.setItemSpecsPrice(clockItemSpecs.getItemSpecsPrice());
                itemSpecs.setItemSpecsTime(clockItemSpecs.getItemSpecsTime());
                itemSpecs.setItemSpecsMemberPrice(clockItemSpecs.getItemSpecsMemberPrice());
                itemSpecsList.add(itemSpecs);
            }
        }
        vo.setItemSpecsList(itemSpecsList);

        return ApiRestResponse.success(vo);
    }
}
